package scratch.InfoEssentials;

import java.awt.image.BufferStrategy;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class NetAccess {
	/* POST Fields */
	// POST 0: submit
	private static String POST_SUBMIT = "submit";
	private static String POST_SUBMIT_VALUE = "";

	// POST 1: lgt
	private static String POST_LGT = "lgt";
	private static String POST_LGT_VALUE = "0"; // default POST_PWUSER_VALUE:0
												// UID:1
												// Email:2

	// POST 2: pwuser
	private static String POST_PWUSER = "pwuser";
	private static String POST_PWUSER_VALUE = "7cc";

	// POST 3: pwpwd
	private static String POST_PWPWD = "pwpwd";
	private static String POST_PWPWD_VALUE = "99577017";

	// POST 4: hideid
	private static String POST_HIDEID = "hideid";
	private static String POST_HIDEID_VALUE = "1"; // hide:1 , not hide:0

	// POST 5: step
	private static String POST_STEP = "step";
	private static String POST_STEP_VALUE = "2";

	// POST 6: jumpurl
	private static String POST_JUMPURL = "jumpurl";
	private static String POST_JUMPURL_VALUE = ""; // TODO

	// POST 7: forward
	private static String POST_FORWARD = "forward";
	private static String POST_FORWARD_VALUE = "";

	// POST 8: cktime
	private static String POST_CKTIME = "cktime";
	private static String POST_CKTIME_VALUE = "31536000"; // year:31536000,
															// month:2592000,
															// day:86400,
															// hour:3600,
															// instant:0
	/* Site Parameters */
	// Site global parameters
	private static String HOSTNAME = "http://bbs.qshpan.com";
	private static String CHARSET = "gb2312";
	private static String METHOD = "post";
	private static String LOGIN_PAGE = "/login.php";
	private static String THREAD_PAGE = "/read.php";
	private static String SEPARATER = "?";

	// cookie details
	private static String COOKIE_NAME = "56f61_winduser";
	private static String COOKIE_VALUE = ""; // BQRUBwowBFMGV15TVgZSAlABBAUHUFkKUQRSVloBVQJVXlVaAVRq

	/* Singleton class NetAccess */
	private static NetAccess netAccess = new NetAccess();

	private NetAccess() {
	}

	public static NetAccess getInstance() {
		return netAccess;
	}

	public static void setCookieValue(String cookieValue) {
		NetAccess.COOKIE_VALUE = cookieValue;
	}

	public static boolean isCookieValid() {
		if (COOKIE_VALUE == null) {
			return false;
		}

		return true;
	}

	// fetch the source file
	public String fetchSource(String url) {
		StringBuffer source = new StringBuffer();
		try {
			HttpURLConnection httpConn = (HttpURLConnection) new URL(url)
					.openConnection();

			String cookie = NetAccess.COOKIE_NAME + "="
					+ NetAccess.COOKIE_VALUE;

			httpConn.setRequestProperty("Cookie", cookie);
			httpConn.connect();

			BufferedReader reader = new BufferedReader(new InputStreamReader(
					httpConn.getInputStream(), NetAccess.CHARSET));

			String line = null;
			while ((line = reader.readLine()) != null) {
				source.append(line);
				source.append("\n");
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return source.toString();
	}

	// login to get cookie
	private boolean login() {
		if (POST_PWUSER_VALUE == null || POST_PWPWD_VALUE == null) {
			return false;
		}

		// refer to login page
		HttpURLConnection httpConn;
		try {
			String postInfo = POST_SUBMIT + "=" + POST_SUBMIT_VALUE + "&"
					+ POST_LGT + "=" + POST_LGT_VALUE + "&" + POST_PWUSER + "="
					+ POST_PWUSER_VALUE + "&" + POST_PWPWD + "="
					+ POST_PWPWD_VALUE + "&" + POST_HIDEID + "="
					+ POST_HIDEID_VALUE + "&" + POST_STEP + "="
					+ POST_STEP_VALUE + "&" + POST_JUMPURL + "="
					+ POST_JUMPURL_VALUE + "&" + POST_FORWARD + "="
					+ POST_FORWARD_VALUE + "&" + POST_CKTIME + "="
					+ POST_CKTIME_VALUE;
			
			httpConn = (HttpURLConnection) new URL(POST_JUMPURL_VALUE).openConnection();
			httpConn.setDoOutput(true);
			httpConn.setRequestMethod("POST");
			httpConn.setUseCaches(false);
			httpConn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 9.0; Windows 2000)");	//ie 9.0
			httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			httpConn.setRequestProperty("Content-Length", String.valueOf(postInfo.length()));
			
			httpConn.setDoInput(true);
			httpConn.setFollowRedirects(true);
			
			httpConn.connect();
			
			
			OutputStreamWriter out = new OutputStreamWriter(httpConn.getOutputStream(),"UTF-8");
			out.write(postInfo);
			out.flush();
			out.close();
			
			System.out.println(httpConn.getResponseCode());
			System.out.println(httpConn.getResponseMessage());
			
			
			String headerName = null;
			for(int i = 1;(headerName = httpConn.getHeaderFieldKey(i)) != null;i++){
				System.out.println(headerName);;
				System.out.println(httpConn.getHeaderField(i));
			}
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),CHARSET));
			
			StringBuffer source = new StringBuffer();
			String line = null;
			while ((line = reader.readLine()) != null) {
				source.append(line);
				source.append("\n");
			}
			
			
			
			System.out.println(source);

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return false;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		NetAccess netAccess = NetAccess.getInstance();
		netAccess.setCookieValue("BQRUBwowBFMGV15TVgZSAlABBAUHUFkKUQRSVloBVQJVXlVaAVRq");
		System.out.println(netAccess.fetchSource("http://bbs.qshpan.com/read.php?tid=1055900"));
//		netAccess.login();
	}
}
